<template>
    <!-- 对话框表单 -->
    <el-dialog custom-class="ba-operate-dialog" :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form v-if="!baTable.form.loading" ref="formRef" @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items" label-position="right" :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules">

                    <el-form-item :label="t('orderGoodsRefund2.refund_sn')">
                        <el-input v-model="baTable.form.items!.refund_sn" readonly />
                    </el-form-item>
                    <el-form-item label="商品">
                        <el-input v-model="baTable.form.items!.orderGoods.goods_name" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.refund_type')">
                        <el-select v-model="baTable.form.items!.refund_type" class="m-2" placeholder="Select" disabled>
                            <el-option :label="t('orderGoodsRefund2.refund_type 0')" :value="0" />
                            <el-option :label="t('orderGoodsRefund2.refund_type 1')" :value="1" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.order_status')">
                        <el-select v-model="baTable.form.items!.order_status" class="m-2" placeholder="Select" disabled>
                            <el-option :label="t('orderGoodsRefund2.order_status 0')" :value="0" />
                            <el-option :label="t('orderGoodsRefund2.order_status 1')" :value="1" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.reason')">
                        <el-input v-model="baTable.form.items!.reason" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.price')" v-if="baTable.form.items!.refund_type == 0">
                        <el-input v-model="baTable.form.items!.price" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.change_attr')" v-if="baTable.form.items!.refund_type == 1">
                        <el-input v-model="baTable.form.items!.change_attr" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.remark')">
                        <el-input v-model="baTable.form.items!.remark" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.username')" v-if="baTable.form.items!.refund_type == 1">
                        <el-input v-model="baTable.form.items!.username" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.mobile')" v-if="baTable.form.items!.refund_type == 1">
                        <el-input v-model="baTable.form.items!.mobile" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.address')" v-if="baTable.form.items!.refund_type == 1">
                        <el-input v-model="baTable.form.items!.address" readonly />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.back_sn')" v-if="baTable.form.items!.refund_type == 1">
                        <el-input v-model="baTable.form.items!.back_sn" />
                    </el-form-item>
                    <el-form-item :label="t('orderGoodsRefund2.shipping_sn')" v-if="baTable.form.items!.refund_type == 1">
                        <el-input v-model="baTable.form.items!.shipping_sn" />
                    </el-form-item>

                    <el-form-item label="">
                        <el-image v-for="src in baTable.form.items!.images" :src="src"
                            style="width: 100px; height: 100px; margin-right: 10px; cursor: pointer;"
                            @click="toShowSlider(baTable.form.items!.images)" fit="cover" />
                    </el-form-item>

                    <FormItem :label="t('orderGoodsRefund2.state')" type="radio" v-model="baTable.form.items!.state"
                        prop="state"
                        :data="{ content: { 0: t('orderGoodsRefund2.state 0'), 1: t('orderGoodsRefund2.state 1'), 2: t('orderGoodsRefund2.state 2'), 3: t('orderGoodsRefund2.state 3') } }"
                        :input-attr="{ placeholder: t('Please select field', { field: t('orderGoodsRefund2.state') }) }" />
                </el-form>
            </div>
            <el-image-viewer v-if="showSlider" :onClose="closeSlider" :url-list="slider"> </el-image-viewer>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    :disabled="state == 1" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
                    t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass
const slider = ref([])
const showSlider = ref(false)

const { t } = useI18n()
const state = ref(0)

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
})
const toShowSlider = (arr: any) => {
    slider.value = arr
    showSlider.value = true
}
const closeSlider = () => {
    showSlider.value = false
}

watch(
    () => baTable.form.items,
    (newVal) => {
        if (newVal.state == 1 || newVal.state === 0) {
            state.value = newVal.state
        }
    })
</script>

<style scoped lang="scss">
</style>
